package com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm;

/**
 * 适用场景,包含选中和未选中的两种状态. 所以View包含两个vm,根据是否选中,来设置当前的vm.
 */

import androidx.annotation.NonNull;

import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemModelDefault;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemStyleDefault;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemStyle;

import java.util.ArrayList;

/**
 * Vm包含两部分信息:
 * 1,模型的内容数据
 * 2,数据的显示样式数据.
 */
public abstract class JsMenuItemVm {
    public boolean mb_isSelected = false;

    //当前vm在整个数组中的序号,实时更新的.
    public int index;
    //当前vm的状态
    //如果两个状态 0未选中,1选中.
    public int mi_state;
    //一共具有的state数.
    public int mi_stateCount;

    private ArrayList<JsMenuItemStyle> mal_style = new ArrayList<>();

    private ArrayList<Object> mal_model = new ArrayList<>();

    private static Object sharedObject = new Object();

    public JsMenuItemVm(int stateCount) {
        mi_stateCount = stateCount;

        for(int i = 0;i < stateCount; i++) {
            mal_model.add(sharedObject);
            mal_style.add(JsMenuItemStyleDefault.sharedInstance());
        }
    }

    public JsMenuItemVm(@NonNull Object model,@NonNull JsMenuItemStyle style) {
        mi_stateCount = 1;
        setModelForState(0,model);
        setStyleForState(0,style);
    }
    public JsMenuItemVm(@NonNull Object model,@NonNull JsMenuItemStyle style,int stateCount) {
        mi_stateCount = stateCount;
        for(int i = 0;i < stateCount; i++) {
            setModelForState(i,model);
            setStyleForState(i,style);
        }
    }

    public JsMenuItemVm(@NonNull Object unselectedModel,@NonNull Object selectedModel,@NonNull JsMenuItemStyle unselectedStyle,@NonNull JsMenuItemStyle selectedStyle) {
        this.mi_stateCount = 2;
        setStyleForState(0,unselectedStyle);
        setStyleForState(1,selectedStyle);
        setModelForState(0,unselectedModel);
        setModelForState(1,selectedModel);
    }

    public Object currentModel() {
       return mal_model.get(mi_state);
    }


    public JsMenuItemStyle currentStyle() {
        return mal_style.get(mi_state);
    }


    public Object getModelForState(int state) {
        return mal_model.get(state);
    }


    public JsMenuItemStyle getStyleForState(int state) {
        return mal_style.get(state);
    }

    public void setStyleForState(int state,JsMenuItemStyle style) {
         mal_style.set(state,style);
    }

    public void setModelForState(int state,Object model) {
        mal_model.set(state,model);
    }

    public JsMenuItemStyle getUnselectedStyle() {
        return getStyleForState(0);
    }

    public void setUnselectedStyle(JsMenuItemStyle unselectedStyle) {
        setStyleForState(0,unselectedStyle);
    }

    public JsMenuItemStyle getSelectedStyle() {
        return getStyleForState(1);
    }

    public void setSelectedStyle(JsMenuItemStyle selectedStyle) {
        setStyleForState(1,selectedStyle);
    }

    public Object getUnselectedModel() {
        return getModelForState(0);
    }

    public void setUnselectedModel(Object unselectedModel) {
        setModelForState(0,unselectedModel);
    }

    public Object getSelectedModel() {
        return getModelForState(1);
    }

    public void setSelectedModel(Object selectedModel) {
        setModelForState(1,selectedModel);
    }

    //设置所有状态的model.
    public void setModel(Object model) {
         for(int i =0;i<mi_stateCount;i++) {
            setModelForState(i,model);
         }
    }
    //设置所有状态的style.
    public void setStyle(JsMenuItemStyle style) {
        for(int i =0;i<mi_stateCount;i++) {
            setStyleForState(i,style);
        }
    }

    public void setModelAndStyle(Object model,JsMenuItemStyle style) {
        for(int i =0;i<mi_stateCount;i++) {
            setModelForState(i,model);
            setStyleForState(i,style);
        }
    }


}
